package com.kaibai.controller;

import com.kaibai.pojo.Good;
import com.kaibai.pojo.Page;
import com.kaibai.service.GoodService;
import com.kaibai.service.impl.GoodServiceImpl;
import com.kaibai.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

public class goodServlet extends BaseServlet {
    private GoodService goodService = new GoodServiceImpl();

    /**
     * 获取全部数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Good> list = goodService.getAll();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/jsp/manager/good_manager.jsp").forward(request,response);
    }

    /**
     * 添加数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),0);
        pageNo+=1;
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        if(!WebUtil.isInteger(price)){
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }
        int sales = WebUtil.parseInt(request.getParameter("sales"),0);
        int stock = WebUtil.parseInt(request.getParameter("stock"),0);
        Good good = new Good(null,name,new BigDecimal(price),sales,stock,null);
        goodService.insert(good);
        //response.sendRedirect(request.getContextPath()+"/manage/good?action=list");
        response.sendRedirect(request.getContextPath()+"/manage/good?action=page&pageNo="+pageNo);
    }

    /**
     * 删除数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        goodService.deleteById(id);
        //response.sendRedirect(request.getContextPath()+"/manage/good?action=list");
        response.sendRedirect(request.getContextPath()+"/manage/good?action=page&pageNo="+request.getParameter("pageNo"));
    }

    /**
     * 通过id获取对象
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getGood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        Good good = goodService.getGoodById(id);
        request.setAttribute("good",good);

        request.getRequestDispatcher("/jsp/manager/good_edit.jsp").forward(request,response);
    }

    /**
     * 更新数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String sales = request.getParameter("sales");
        String stock = request.getParameter("stock");
        Good good = new Good(id,name,new BigDecimal(price),Integer.valueOf(sales),Integer.valueOf(stock),null);
        goodService.update(good);

        //response.sendRedirect(request.getContextPath()+"/manage/good?action=list");
        response.sendRedirect(request.getContextPath()+"/manage/good?action=page&pageNo="+request.getParameter("pageNo"));
    }

    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取参数
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用service
        Page<Good> page = goodService.page(pageNo,pageSize);
        page.setUrl("good?action=page");
        //3.保存在request域中
        request.setAttribute("page",page);
        request.getRequestDispatcher("/jsp/manager/good_manager_page.jsp").forward(request,response);
    }

    protected void pageBySearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = request.getParameter("name").trim();
        int min = WebUtil.parseInt(request.getParameter("min"),0);
        int max = WebUtil.parseInt(request.getParameter("max"),Integer.MAX_VALUE);
        Page<Good> page = goodService.pageBySearch(pageNo,pageSize,name,min,max);
        String url = "good?action=pageBySearch&name="+name+"&min="+min+"&max="+max;
        page.setUrl(url);
        request.setAttribute("page",page);
        request.getRequestDispatcher("/jsp/manager/good_manager_page.jsp").forward(request,response);
    }

}
